package com.amor.ui.fragment

import android.view.View
import android.webkit.WebSettings
import com.amor.app.BaseFragment
import com.amor.app.MyApplication
import com.amor.demo.web1.BR
import com.amor.demo.web1.R
import com.amor.demo.web1.databinding.FHomeBinding
import com.amor.ui.*
import com.amor.vm.HomeViewModel
import com.amor.widget.ExWebView
import com.hy.frame.ui.ToolbarUI
import com.hy.frame.util.ActUtil
import com.hy.frame.util.LogUtil

/**
 * title 主页
 * author HeYan
 * date 2021/9/26
 * desc
 */
class HomeFragment : BaseFragment<FHomeBinding, HomeViewModel>() {
    private var toolbar: ToolbarUI? = null
    override val layoutId: Int = R.layout.f_home

    override val viewModel: Class<HomeViewModel> = HomeViewModel::class.java

    override val variableId: Int = BR.model

    override fun initView() {
        this.toolbar = com.amor.widget.ToolbarUI(requireContext()).apply {
            setTitle(R.string.appName)
            setTranslucentStatus(true)
            build(mBinding?.layout)
        }
    }

    override fun initData() {

    }

    override fun initObserver() {
        super.initObserver()
        mModel?.click?.value = View.OnClickListener {
            when (it.id) {
                R.id.btn_test1 -> {
                    ActUtil.startAct(context, WebActivity::class.java)
                }
                R.id.btn_test2 -> {
                    ActUtil.startAct(context, CacheWebActivity::class.java)
                }
                R.id.btn_test3 -> {
                    ActUtil.startAct(context, StaticWebActivity::class.java)
                }
                R.id.btn_test4 -> {
                    val web = MyApplication.get().getWebViewHolder().build(PreWebActivity.WEB_FLAG)
                    web.strip()
                    web.initWebSettings(WebSettings.LOAD_DEFAULT)
                    if (web.isPrePageFinished(PreWebActivity.URL, true)) {
                        ActUtil.startAct(context, PreWebActivity::class.java)
                    } else {
                        showLoading("预加载...")
                        web.preLoad(PreWebActivity.URL, object : ExWebView.PreCallback {
                            override fun onPrePageStarted(url: String) {
                                LogUtil.d(javaClass, "预加载：开始$url")
                            }

                            override fun onPrePageFinished(url: String) {
                                LogUtil.d(javaClass, "预加载：完成$url")
                                //showLoading("预加载完成")
                                dismissLoading()
                                ActUtil.startAct(context, PreWebActivity::class.java)
                            }

                            override fun onPrePageError(url: String) {
                                LogUtil.d(javaClass, "预加载：失败$url")
                                dismissLoading()
                                showToast("预加载失败")
                            }
                        })
                    }

                }
                R.id.btn_test5 -> {
                    multiPreLoad()
                }
                R.id.btn_test6 -> {
                    ActUtil.startAct(context, ProcessWebActivity::class.java)
                }

            }
        }
    }

    private var multiUrl: ArrayList<String> = ArrayList()
    private fun multiPreLoad() {
        multiUrl.clear()
        val web = MyApplication.get().getWebViewHolder().build(MultiPreWebActivity.WEB_FLAG)
        web.strip()
        web.initWebSettings(WebSettings.LOAD_DEFAULT)
        multiUrl.addAll(MultiPreWebActivity.URL_LIST)
        showLoading("预加载...")
        multiPreLoadEach(web)

    }

    private fun multiPreLoadEach(web: ExWebView) {
        for (item in multiUrl) {
            if (web.isPrePageFinished(item, false))
                continue
            multiPreLoadIMPL(web, item)
            return
        }
        //判断一级页面是否加载完成
        if (!web.isPrePageFinished(MultiPreWebActivity.URL, true)) {
            multiPreLoadIMPL(web, MultiPreWebActivity.URL)
            return
        }
        dismissLoading()
        ActUtil.startAct(context, MultiPreWebActivity::class.java)
    }

    private fun multiPreLoadIMPL(web: ExWebView, url: String) {
        web.preLoad(url, object : ExWebView.PreCallback {
            override fun onPrePageStarted(url: String) {
                LogUtil.d(javaClass, "预加载：开始$url")
            }

            override fun onPrePageFinished(url: String) {
                LogUtil.d(javaClass, "预加载：完成$url")
                //showLoading("预加载完成")
                multiPreLoadEach(web)
            }

            override fun onPrePageError(url: String) {
                LogUtil.d(javaClass, "预加载：失败$url")
                dismissLoading()
                showToast("预加载失败")
            }
        })
    }
}